package com.mr.web.support;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.mr.service.MailService;
import com.mr.util.DateUtil;

public class PageExceptionResolver extends SimpleMappingExceptionResolver {
	@Autowired@Qualifier("mailService")
	protected MailService mailService;
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}
	@Override
	protected String buildLogMessage(Exception ex, HttpServletRequest request) {
		Enumeration params = request.getParameterNames();
		String msg = "�д�����!" + encodeName("xxxx:");
		String tmpname;
		while (params.hasMoreElements()) {
			tmpname = (String) params.nextElement();
			msg += tmpname + "=" + request.getParameter(tmpname) + "&";
		}
		msg += "\nRequest Header:";
		for (Enumeration names = request.getHeaderNames(); names
				.hasMoreElements();) {
			tmpname = (String) names.nextElement();
			msg += tmpname + "=" + request.getHeader(tmpname) + ", ";
		}
		msg += "\nRequest URL:" + request.getRequestURL().toString();
		return msg;
	}

	protected void sendExceptionMessage(Exception ex, HttpServletRequest request) {
   	String host = request.getHeader("X-Forwarded-Host");
      StringWriter sw = new StringWriter();
      if(ex instanceof MissingServletRequestParameterException){
      	if(StringUtils.isBlank(request.getHeader("Referer"))) return;
      	sw.append("ȱ�ٲ���" + ex.getMessage());
      }else if(ex instanceof TypeMismatchException){
      	if(StringUtils.isBlank(request.getHeader("Referer"))) return;
      	sw.append("�������ʹ���" + ex.getMessage());
      }else{
      	ex.printStackTrace(new PrintWriter(sw));
      }
      Enumeration params = request.getParameterNames();
      String msg = "�д�����!\n" + encodeName("���ݵĲ���:");
      String tmpname;
      String paramsStr = "";
      while(params.hasMoreElements()){
         tmpname = (String) params.nextElement();
         paramsStr += tmpname + "=" + request.getParameter(tmpname) + "&";
         msg +=  encodeName(tmpname) + "=" + request.getParameter(tmpname) + "&";
      }
      msg +="\n\n<span style='color:green'>Request Header:</span>\n";
      for(Enumeration names = request.getHeaderNames(); names.hasMoreElements();){
      	tmpname = (String) names.nextElement();
      	msg += "\t" + encodeName(tmpname) + "=" + request.getHeader(tmpname) + ",";
      }
      msg +="\n\n<span style='color:green'>Exception:</span>\n";
      msg += sw.toString();
      msg = StringUtils.replace(msg, "\n", "<br />");
   	String subject = request.getRequestURI() + "?" + paramsStr + "@" + host + "," + DateUtil.formatTime(new Date());
   	try {
   		mailService.sendEmail("liqilun@126.com", subject, msg);
      } catch (Exception e) {
      }
	}
	@Override
	protected void logException(Exception ex, HttpServletRequest request) {
		sendExceptionMessage(ex, request);
		logger.error(buildLogMessage(ex, request), ex);
	}

	private String encodeName(String name) {
		return "<span style='color:red'>" + name + "</span>";
	}
}
